package com.ml.mall.conversion;

import com.ml.mall.bean.req.BaseDateRangeReq;
import com.ml.mall.bean.res.admin.PurchaseQueryOneRes;
import com.ml.mall.bean.res.admin.vo.AdminOverviewSalesByCategoryVO;
import com.ml.mall.bean.res.purchase.PurchaseOverviewRes;
import com.ml.mall.bean.res.purchase.PurchaseSpentQueryReportRes;
import com.ml.mall.bean.res.purchase.PurchaseSupplierQueryReportRes;
import com.ml.mall.bean.res.purchase.vo.PurchaseCategoryTopVO;
import com.ml.mall.bean.res.purchase.vo.PurchaseSupplierVO;
import com.ml.mall.bean.res.purchase.vo.PurchaseVariantVO;
import com.ml.mall.bean.vo.DateValueVO;
import com.ml.mall.utils.DateTransformUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

/**
 * Created by kyyang on 2021/6/9 23:54
 */

public class AnalyticsPurchaseServiceHelp {



    public static PurchaseOverviewRes getPurchaseOverviewConversion(Future<List<AdminOverviewSalesByCategoryVO>> adminOverviewSalesByCategoryVOFuture, Future<List<PurchaseSupplierVO>> purchaseSupplierVOFuture, Future<List<PurchaseVariantVO>> purchaseVariantVOFuture) throws ExecutionException, InterruptedException {

        PurchaseOverviewRes res = new PurchaseOverviewRes();
        if(adminOverviewSalesByCategoryVOFuture!=null){
            List<AdminOverviewSalesByCategoryVO> adminOverviewSalesByCategoryVOS = adminOverviewSalesByCategoryVOFuture.get();
            res.setSalesByCategory(adminOverviewSalesByCategoryVOS);
        }
        if(purchaseSupplierVOFuture!=null){
            List<PurchaseSupplierVO> purchaseSupplierVOS = purchaseSupplierVOFuture.get();
            res.setSalesBySupplier(purchaseSupplierVOS);
        }
        if(purchaseVariantVOFuture!=null){
            List<PurchaseVariantVO> purchaseVariantVOS = purchaseVariantVOFuture.get();
            res.setSalesByVariant(purchaseVariantVOS);
        }

        return res;

    }

    public static PurchaseSpentQueryReportRes getPurchaseSpentQueryReportConversion(PurchaseQueryOneRes purchaseQueryOneRes, Future<List<DateValueVO>> purchaseVariantVOFuture, BaseDateRangeReq req) throws ExecutionException, InterruptedException {
        PurchaseSpentQueryReportRes res = new PurchaseSpentQueryReportRes();
        if(purchaseQueryOneRes!=null){
            res.setTotalSpent(purchaseQueryOneRes.getTotalSpent());
            res.setTotalPo(purchaseQueryOneRes.getTotalPo());
            res.setPoDayDetail(purchaseQueryOneRes.getTotalPoDetail());
            res.setSpentDayDetail(purchaseQueryOneRes.getSpentDetail());
        }
        if(purchaseVariantVOFuture!=null){
            List<PurchaseCategoryTopVO> resList = new ArrayList<>();
            List<DateValueVO> dateValueVOS = purchaseVariantVOFuture.get();
            if(dateValueVOS!=null&&dateValueVOS.size()>0){
                Map<String, List<DateValueVO>> collect = dateValueVOS.stream().collect(Collectors.groupingBy(DateValueVO::getName));
                for(Map.Entry<String, List<DateValueVO>> entry: collect.entrySet()){
                    PurchaseCategoryTopVO purchaseOrderArrVO = new PurchaseCategoryTopVO();
                    String key = entry.getKey();
                    List<DateValueVO> value = entry.getValue();
                    purchaseOrderArrVO.setCategoryName(key);
                    Double v = value.stream().mapToDouble(e->e.getValue().doubleValue()).sum();
                    purchaseOrderArrVO.setTotalSpent(BigDecimal.valueOf(v));
                    Map<String, Double> result = value.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
                    List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), result);
                    purchaseOrderArrVO.setDayDetail(curDateMonthDataInit);
                    resList.add(purchaseOrderArrVO);
                }
            }
            resList = resList.stream().sorted(Comparator.comparing(PurchaseCategoryTopVO::getTotalSpent).reversed()).collect(Collectors.toList());
            if(resList.size()>10){
                resList = resList.subList(0,10);
            }
            res.setTop10SpentByCategory(resList);
        }
        return res;

    }

    public static List<PurchaseSupplierQueryReportRes> getPurchaseSupplierQueryReportConversion(Future<List<DateValueVO>> purchaseSupplierVOFuture, BaseDateRangeReq req)throws Exception {

        List<PurchaseSupplierQueryReportRes> resList = new ArrayList<>();
        if(purchaseSupplierVOFuture!=null){
            List<DateValueVO> dateValueVOS = purchaseSupplierVOFuture.get();
            if(dateValueVOS!=null&&dateValueVOS.size()>0){
                Map<String, List<DateValueVO>> collect = dateValueVOS.stream().collect(Collectors.groupingBy(DateValueVO::getName));
                for(Map.Entry<String, List<DateValueVO>> entry: collect.entrySet()){
                    PurchaseSupplierQueryReportRes purchaseOrderArrVO = new PurchaseSupplierQueryReportRes();
                    String key = entry.getKey();
                    List<DateValueVO> value = entry.getValue();
                    purchaseOrderArrVO.setSupplierCode(key);
                    Double v = value.stream().mapToDouble(e->e.getValue().doubleValue()).sum();
                    purchaseOrderArrVO.setTotalSpent(BigDecimal.valueOf(v));
                    Map<String, Double> result = value.stream().collect(Collectors.groupingBy(e -> e.getYmd(), Collectors.summingDouble(g -> g.getValue().doubleValue())));
                    List<DateValueVO> curDateMonthDataInit = DateTransformUtils.getCurDateMonthDataInit(req.getTimeBegin(), req.getTimeEnd(), result);
                    purchaseOrderArrVO.setDayDetail(curDateMonthDataInit);
                    resList.add(purchaseOrderArrVO);
                }
            }
            resList = resList.stream().sorted(Comparator.comparing(PurchaseSupplierQueryReportRes::getTotalSpent).reversed()).collect(Collectors.toList());
            if(resList.size()>10){
                resList = resList.subList(0,10);
            }
        }
        return resList;
    }
}
